package com.yjyw.tiexie.util;

import android.content.Context;
import android.os.Environment;

import java.io.File;

public class FileStorageManager {
    // 500M
    public static final long MAX_SONG_CACHE_SIZE_IN_BYTES = 500L * 1024L * 1024L;

    public static String getInternalFilesRootPath(Context context) {
        return context.getFilesDir().getAbsolutePath();
    }

    public static String getInternalCacheRootPath(Context context) {
        return context.getCacheDir().getAbsolutePath();
    }

    // Returns absolute paths to application-specific directories on all shared/external storage devices
    // where the application can place persistent files it owns.
    // These files are internal to the application, and not typically visible to the user as media.
    // 该目录下面的文件会随app卸载时删除，并且在media不可见
    public static String getExternalAppRootPath(Context context) {
        File file = context.getExternalFilesDir(null);
        if (file != null) {
            return file.getAbsolutePath();
        }
        return null;
    }

    public static String getExternalAppRootCachePath(Context context) {
        File file = context.getExternalCacheDir();
        if (file != null) {
            return file.getAbsolutePath();
        }
        return null;
    }

    public static String getExternalRootPath() {
        return Environment.getExternalStorageDirectory().getAbsolutePath();
    }

    public static String getAppDefaultStorageRootPath(Context context) {
        String path = getExternalAppRootPath(context);
        if (!StringUtil.isEmpty(path)) {
            return path;
        }

        return getExternalRootPath() + "/perdo/files";
    }

    public static String getAppDefaultCacheRootPath(Context context) {
        String path = getExternalAppRootCachePath(context);
        if (!StringUtil.isEmpty(path)) {
            return path;
        }

        return getExternalRootPath() + "/perdo/cache";
    }

    public static String getApkStoragePath(Context context) {
        return getAppDefaultStorageRootPath(context) + "/apk/";
    }

    public static String getAppRecordCachePath(Context context) {
        return getAppDefaultCacheRootPath(context) + "/record/";
    }

    public static String getAppVideoCachePath(Context context) {
        return getAppDefaultCacheRootPath(context) + "/video/";
    }

    public static String getAppImageCachePath(Context context) {
        return getAppDefaultCacheRootPath(context) + "/image/";
    }

    public static String getCropImageTmpPath(Context context) {
        return getAppImageCachePath(context) + "/cropped_tmp.png";
    }

    public static String getLogDirPath(Context context) {
        String dir = getAppDefaultCacheRootPath(context) + "/logs/";

        File dirFile = new File(dir);
        if (!dirFile.exists()) {
            dirFile.mkdir();
        }

        return dir;
    }
}
